package net.wanho.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.wanho.entity.User;
import net.wanho.service.UserService;
import net.wanho.vo.AjaxResult;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Author：汤小洋
 * Date：2023-09-27 9:27
 * Description：<描述>
 */
@Api(tags = "01.用户模块")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @ApiOperation("添加用户")
    @PostMapping
    public AjaxResult add(@ApiParam("用户信息") @RequestBody User user){
        userService.save(user);
        return AjaxResult.success(user);
    }

    @ApiOperation("根据id删除用户")
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@ApiParam("用户id") @PathVariable Long id){
        userService.removeById(id);
        return AjaxResult.success();
    }

    @ApiOperation("根据id修改用户")
    @PutMapping
    public AjaxResult update(@ApiParam("用户信息") @RequestBody User user){
        userService.updateById(user);
        return AjaxResult.success();
    }

    @ApiOperation("查询所有用户")
    @GetMapping
    public AjaxResult list(){
        return AjaxResult.success(userService.list());
    }

    @GetMapping("/{id}")
    public AjaxResult findById(@PathVariable Long id){
        return AjaxResult.success(userService.getById(id));
    }

    @PostMapping("/login")
    public AjaxResult login(@RequestBody User user){
        User u = userService.login(user);
        return AjaxResult.success(u);
    }

    @GetMapping("/query")
    public AjaxResult query(@RequestParam String username){
        /*QueryWrapper<User> wrapper = new QueryWrapper<>();
        // wrapper.like("username",username); // username like '%username%'
        // wrapper.likeRight("username",username); // username like 'username%'
        wrapper.likeRight(!StringUtils.isEmpty(username), "username", username); // 动态SQL
        wrapper.or(); // 表示或者
        // wrapper.ge("id", 1);
        // wrapper.between("id", 2, 5);
        wrapper.in("id", 1, 2, 3, 4, 5);
        wrapper.orderByDesc("id");  // 排序*/

        // 使用Lambda语法
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(User::getUsername, username) // 方法引用，可以避免写错字段名
                .eq(User::getId,1)
                .eq(User::getPassword,"123456")
                .orderByDesc(User::getId);

        return AjaxResult.success( userService.list(wrapper));
    }

    @ApiIgnore
    @GetMapping("/page")
    public AjaxResult page(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "5") Integer pageSize,
                           @RequestParam String username){
        Page<User> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(User::getUsername, username);

        userService.page(page, wrapper);

        return AjaxResult.success(page);
    }

}
